/**
 *@author:温镇澎
 */
$(document).ready(function() {
	firstLoadUser();
	findAllRole();
	findAllUserState();
	findAllDeptType();
	//重置查询条件
	$("#q_rest").click(function(){
		document.getElementById("q_URole").selectedIndex=0;
		document.getElementById("q_UState").selectedIndex=0;
		document.getElementById("deptType").selectedIndex=0;
		$("#q_UDept").empty();
		$("#q_UId").val("");
		$("#q_UName").val("")
	});
  	$("#deptType").change(function() {
		var dtId = $("#deptType option:selected").val();
		$.ajax({
  			type: "post",
  			data:{"dtId":dtId},
 			url: "admin/userDept_findUserDeptByDtId.action",
  			success:function(data){
  				$("#q_UDept").empty();
  				$("#q_UDept").append("<option value='0'>请选择</option>");
  				$.each(data.userDepts,function(i,n){
  					$("#q_UDept").append("<option value="+n.udId+">"+n.udName+"</option>");
				});
  			}
  		});
	});
	//根据条件查询 
	$("#query").click(function(){
		doAjax(getParams(1));
	});
	//分页查询，查第一页
	$("#firstPage").click(function(){
		doAjax(getParams(1));
	});
	//分页查询，查上一页
	$("#prePage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)-1);
		if(page<=0){
			$("#message").text("这是第一页，没有上一页了！").css("color","red");
			showMessage();
			return;
		}
		doAjax(getParams(page));
	});
	//分页查询，查下一页
	$("#nextPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)+1);
		if(page>pageCount){
			$("#message").text("这是最后一页，没有下一页了！").css("color","red");
			showMessage();
			return;
		}
		doAjax(getParams(page));
	});
	//分页查询，查最后一页
	$("#lastPage").click(function(){
		var page=$("#pageCount").text();
		doAjax(getParams(page)); 
	});
	//分页查询，跳转到第n页
	$("#jumpPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#jump").val();
		if(page>pageCount){
			$("#message").text("要跳转的页数比总页数要大，请选择较小的页数！").css("color","red");
			showMessage();
			return;
		} 
		doAjax(getParams(page)); 
	});
});	
	//获取需要的参数
	function getParams(p){
		$("#listUser").empty();
		$("#loadingUser").show();
		var page=p;
		var maxRow=$("#maxRow").text();
		var q_URole = $.trim($("#q_URole").val());
		var q_UDept = $.trim($("#q_UDept").val());
		var q_UState = $.trim($("#q_UState").val());
		var q_UId = $.trim($("#q_UId").val());
		var q_UName = $.trim($("#q_UName").val());
		params = {
				"queryOption['page']":page,
				"queryOption['maxRow']":maxRow,
				"queryOption['URole']":q_URole,
				"queryOption['UDept']":q_UDept,
				"queryOption['UState']":q_UState,
		 		"queryOption['UId']":q_UId,
				"queryOption['UName']":q_UName
				};
		return params;
	}
	
	//分页查询，异步提交
	function doAjax(d){
		$.ajax({
			type : "post",
			url : "admin/queryUser_query.action",
			data : d,
			success : showData
		});
	}
	//获取所有角色
	function findAllRole(){
		$.ajax({
  			type: "post",
 			url: "admin/Role_findAllRole.action",//因为com.ag.admin.action.RoleAction的controller配的是RoleAction
  			success:function(data){
  				$("<option value='0'>请选择</option>").appendTo(".uRole");
  				$.each(data.roles,function(i,n){
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo(".uRole");
  					//数据添加到添加用户时选择角色的地方
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo("#allRole");
				});
  			}
  		});
	}
	//获取所有用户状态
	function findAllUserState(){
		$.ajax({
  			type: "post",
 			url: "admin/user_findAllUserState.action",
  			success:function(data){
  				$("<option value='0'>请选择</option>").appendTo(".uState");
  				$.each(data.userStates,function(i,n){
  					$("<option value="+n.usId+">"+n.usName+"</option>").appendTo(".uState");
  					//数据添加到添加用户时选择角色的地方
  					$("<option value="+n.usId+">"+n.usName+"</option>").appendTo("#selUState");
				});
  			}
  		});
	}
	
	//获取所有部门类型
	function findAllDeptType(){
		$.ajax({
  			type: "post",
 			url: "admin/userDept_findAllDeptType.action",
  			success:function(data){
  				$("<option value='0'>请选择</option>").appendTo("#deptType");
  				$("#q_UDept").append("<option value='0'></option>");
  				$.each(data.deptTypes,function(i,n){
  					$("<option value="+n.dtId+">"+n.dtName+"</option>").appendTo("#deptType");
				});
  			}
  		});
	}
//第一次查询，无参数查询
	function firstLoadUser(){
		$.ajax( {
			type : "post",
			url : "admin/queryUser_query.action",
			success : showData
		});
	}
	//根据用户登录名删除用户
	function deleteUser(uId){
		/*$("#message").text("删除成功！").css("color","red");
		$("#confirm").text("你确定要删除登录名是"+uId+"的用户？").css("color","red");
		$("#dialog-confirm").dialog('open');
		$("#dialog-confirm").dialog({
			autoOpen : false,
			height:200,
			width: 300,
			modal: true,
			resizable: false,
			buttons: {
				'确定': function() {
					$.ajax( {
						type : "post",
						data :{"user.UId":uId},
						url : "admin/user_delete.action",
						success : function(data){
							if(data){
								$("#message").text("删除成功！").css("color","red");
								showMessage();
								doAjax(getParams(1));
							}
						}
					});
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			}
		});*/
		if(confirm("你确定要删除登录名是"+uId+"的用户？")){
			$.ajax( {
				type : "post",
				data :{"user.UId":uId},
				url : "admin/user_delete.action",
				success : function(data){
					if(data){
						$("#message").text("删除成功！").css("color","red");
						showMessage();
						doAjax(getParams(1));
					}
				}
			});
		}
	};
	
	function showData(data){
		$("#listUser").empty().hide();
		$("#noneUser").hide();
		$("#loadingUser").hide();
		if(data.pageUtil.list==null){
			$("#total").text(0);
			$("#page").text(0);
			$("#pageCount").text(0);
			$("#maxRow").text(0);
			$("#noneUser").show();
		}else{
			$.each(data.pageUtil.list,function(i,n){
				if(n.tbUserState!=null)
					fillDataToHtml(n.UId, n.UName, n.tbUserRoles, n.tbUserDepts,n.ULoginTime, n.tbUserState.usName);
			});
			$("#listUser").show();
			$("#total").text(data.pageUtil.total);
			$("#page").text(data.pageUtil.page);
			$("#pageCount").text(Math.ceil(data.pageUtil.total/10));
			$("#maxRow").text(data.pageUtil.maxRow);
		}
	}
	
	function fillDataToHtml(uId,uName,tbUserRoles,tbUserDepts,uLoginTime,usName){
		var uRoles=new Array(tbUserRoles.length);
		$.each(tbUserRoles,function(i,n){
			uRoles[i] = "  "+n.tbRole.RName ;
		});
		var uDepts=new Array(tbUserDepts.length);
		var lDepts=new Array();
		$.each(tbUserDepts,function(i,n){
//			if(n.tbUser==null){
//				//lDepts[i] = "  "+n.udName+"（负责人）";
//				lDepts.push("  "+n.udName);
//			}	
			uDepts[i]="  "+n.udName ;
		});
		var loginTime="";
		if(uLoginTime!=null){
			loginTime=uLoginTime.replace(/T/,' ');
		}
		var strHtml="<tr class='truser'>";
		strHtml+="<td ><div><a href='/ag/admin/user_findUserByUId.action?user.UId="+uId+"'>"+uId+"</a></div></td>";
        strHtml+="<td ><div>"+uName+"</div></td>";
        strHtml+="<td ><div>"+uRoles+"</div></td>";
        strHtml+="<td ><div>"+uDepts+"</div></td>";
        //strHtml+="<td ><div>"+lDepts+"</div></td>";
        strHtml+="<td ><div>"+loginTime+"</div></td>";
        strHtml+="<td ><div>"+usName+"</div></td>";
        strHtml+="<td ><div><input type='button' onclick='window.location.href=\"/ag/admin/user_editUser.action?user.UId="+uId+"\"' value='编辑'/></div></td>";
        strHtml+="<td ><div><input type='button' onclick='deleteUser(\""+uId+"\")' value='删除'/></div></td>";
        strHtml+="</tr>";
        $("#listUser").append(strHtml);
        //changetTrColor(".truser");
	}
	/**
	 * 显示出错信息
	 * @memberOf {TypeName} 
	 */
	function showMessage(){
		$("#dialog").dialog("destroy");
		$("#dialog-message").dialog({
			height:200,
			width: 300,
			modal: true,
			resizable: false,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});
	}
	